package com.atguigu.commo.handler;

import com.atguigu.commo.exception.StgyException;
import com.atguigu.commo.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class StgyGlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public Result exception(Exception e){
        log.error(ExceptionUtils.getStackTrace(e));
        return Result.fail();
    }
    @ExceptionHandler(value = StgyException.class)
    public Result exception(StgyException e){
        if ( e.getThrowable()!= null) {
            log.error(ExceptionUtils.getStackTrace(e.getThrowable()));
        }
        return  Result.build(e.getCode(), e.getMessage(), false, null);
    }
}
